library ieee;
use ieee.std_logic_1164.all;

-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
use ieee.numeric_std.all;

-- Uncomment the following library declaration if instantiating
-- any Xilinx leaf cells in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity leds_controller is
    generic (
        LED_LOW  : integer := 0;
        LED_HIGH : integer := 15
    );
    port (
        clk : in std_logic;
        res : in std_logic;

        tick     : in std_logic;
        cnt_secs : in integer;

        led : out std_logic_vector(LED_HIGH downto LED_LOW)
    );
end leds_controller;

architecture Behavioral of leds_controller is

    constant INITIAL_LED_INTERNAL : std_logic_vector(LED_HIGH downto LED_LOW) := "0000000000000000";

    signal led_internal      : std_logic_vector(LED_HIGH downto LED_LOW) := "0000000000000000";
    signal led_internal_next : std_logic_vector(LED_HIGH downto LED_LOW) := "0000000000000000";
    
    type t_cycle is array (LED_LOW to LED_HIGH) of integer;

begin

    led <= led_internal;

    reg : process (clk, res)
    begin
        -- default assignment
        --led_internal <= led_internal;

        -- register like functionality
        if res = '1' then
            led_internal <= INITIAL_LED_INTERNAL;
        elsif rising_edge(clk) then
            -- assign next set of led states
            led_internal <= led_internal_next;
        end if;
    end process;

    blink : process (all)
    begin
        -- default assignment
        led_internal_next <= led_internal_next;

        -- display binary number using leds
        if tick = '1' then
            -- do this for all leds from LED_LOW to LED_HIGH
            for i in LED_HIGH downto LED_LOW loop
                -- let type conversion do the job to represent our int
                -- as a number and access correct index
                led_internal_next(LED_HIGH - i) <= to_unsigned(cnt_secs, LED_HIGH + 1)(i);
            end loop;
        end if;
    end process;

end Behavioral;
